package com.loongtech.bi.action.newsystem;

import com.loongtech.bi.action.authManagement.ResultData;
import com.loongtech.bi.entity.POJO.Page;
import com.loongtech.bi.entity.system.EntityGameParamType;
import com.loongtech.bi.manager.system.GameParamConfigManager;
import com.loongtech.bi.manager.system.GameParamTypeConfigManager;
import com.loongtech.bi.manager.system.SyncInfoLogManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.core.util.RetCode;
import com.loongtech.core.util.Time;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author: yuri
 * Description: 游戏参数类型配置
 * Date: 10:45 2017/5/24
 */
@RestController
@Scope("prototype")
@RequestMapping("/gameparamtype")
public class GameParamsTypeConfigAction {
    @Resource
    private GameParamTypeConfigManager gameParamTypeManager;
    @Resource
    private GameParamConfigManager gameParamConfigManager;
    @Resource
    private Session session;
    @Resource
    private SysLogManager sysLogManager;
    @Resource
    private SyncInfoLogManager syncInfoLogManager;

    /**
     * 查询
     */
    @RequestMapping(value = "list.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<?> searchByCriteria(@RequestParam Integer curPage, @RequestParam Integer pageSize, EntityGameParamType entity) {
        Page<List<EntityGameParamType>> list;
        try {
            list = gameParamTypeManager.queryByCriteria(curPage, pageSize, entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
        return new ResultData<>(RetCode.eSuccess.getErrorcode(), list);
    }

    @RequestMapping(value = "add.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> add(EntityGameParamType entity) {
//        if (Utils.isEmptyStr(entity.getGameName())) {
//            return new ResultData<>(RetCode.eEmptyGameName.getErrorcode(), RetCode.eEmptyGameName.getErrorMsg());
//        }
        if (entity.getGameId() == null) {
            return new ResultData<>(RetCode.eEmptyGameId.getErrorcode(), RetCode.eEmptyGameId.getErrorMsg());
        }
        if (StringUtils.isEmpty(entity.getName())) {
            return new ResultData<>(RetCode.eNotExistName.getErrorcode(), RetCode.eNotExistName.getErrorMsg());
        }
        try {
            if (GameParamTypeConfigManager.canAdd(entity)) {
                gameParamTypeManager.persist(entity);
                StringBuilder insertSQL = new StringBuilder("insert into bi_game_param_type(gameId,name,open,updateAt) values");
                insertSQL.append("(").append("'").append(entity.getGameId()).append("','").append(entity.getName()).append("',").append(entity.isOpen()).append(",").append(Time.currentTime()).append(")");
                String gameId = entity.getGameId();
                for (String gid : gameId.split(",")) {
                    syncInfoLogManager.add(gid, insertSQL.toString());
                }
                gameParamTypeManager.init();
                return ResultData.success();
            } else {
                return new ResultData<>(RetCode.eNameExist.getErrorcode(), RetCode.eNameExist.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "modify.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> modify(EntityGameParamType entity) {
//        if (Utils.isEmptyStr(entity.getGameName())) {
//            return new ResultData<>(RetCode.eEmptyGameName.getErrorcode(), RetCode.eEmptyGameName.getErrorMsg());
//        }
        if (entity.getGameId() == null) {
            return new ResultData<>(RetCode.eEmptyGameId.getErrorcode(), RetCode.eEmptyGameId.getErrorMsg());
        }
        if (StringUtils.isEmpty(entity.getName())) {
            return new ResultData<>(RetCode.eNotExistName.getErrorcode(), RetCode.eNotExistName.getErrorMsg());
        }
        try {
            EntityGameParamType cur = gameParamTypeManager.get(entity.getId());
            if (cur != null) {
                if (GameParamTypeConfigManager.canAdd(entity)) {
                    gameParamTypeManager.merge(entity);
                    sysLogManager.addLog(session.getUserName(), "游戏参数类型修改成功:" + entity.getName());
                    StringBuilder updateSQL = new StringBuilder("update bi_game_param_type set name = ");
                    updateSQL.append("'").append(entity.getName()).append("',open = ").append(entity.isOpen()).append(",updateAt = ").append(Time.currentTime()).append(" where  id = ").append(entity.getId());
                    String gameId = entity.getGameId();
                    for (String gid : gameId.split(",")) {
                        syncInfoLogManager.add(gid, updateSQL.toString());
                    }
                    gameParamTypeManager.init();
                    return ResultData.success();
                } else {
                    gameParamTypeManager.init();
                    return new ResultData<>(RetCode.eNameExist.getErrorcode(), RetCode.eNameExist.getErrorMsg());
                }
            } else {
                return new ResultData<>(RetCode.eNotExistGourp.getErrorcode(), RetCode.eNotExistGourp.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }

    @RequestMapping(value = "delete.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData<String> delete(@RequestParam Integer id) {
        Object res = gameParamConfigManager.getByQuery("from EntityGameParam where paramtype = ?", id);
        if (res != null) {
            return new ResultData<>(RetCode.eRelationExist.getErrorcode(), RetCode.eRelationExist.getErrorMsg());
        }
        try {
            EntityGameParamType cur = gameParamTypeManager.get(id);
            if (cur != null) {
                gameParamTypeManager.removeById(id);
                for (String gid : cur.getGameId().split(",")) {
                    syncInfoLogManager.add(gid, "delete from bi_game_param_type where id = " + cur.getId());
                }
                sysLogManager.addLog(session.getUserName(), "游戏参数类型删除成功:" + id);
                gameParamTypeManager.init();
                return ResultData.success();
            } else {
                return new ResultData<>(RetCode.eNotExistGourp.getErrorcode(), RetCode.eNotExistGourp.getErrorMsg());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResultData.otherException();
        }
    }
}
